# Add new item
import tkinter as tk
from tkinter import messagebox
from src.core import items as items_core
from src.ui.tkinter.screens.launch_screen import show_launch_screen

def show_creation_screen(parent, item_manager):
    """Display the creation screen"""
    # Clear window
    for widget in parent.winfo_children():
        widget.destroy()
    
    # Header
    header_frame = tk.Frame(parent, bg="#F5E6D3")
    header_frame.pack(fill=tk.X, pady=10)
    
    title = tk.Label(header_frame, text="➕ Add New Bakery Item", 
                     font=("Arial", 24, "bold"), 
                     bg="#F5E6D3", fg="#4A3728")
    title.pack(side=tk.LEFT, padx=20)
    
    back_btn = tk.Button(header_frame, text="← Back", 
                         command=lambda: show_launch_screen(parent, item_manager),
                         font=("Arial", 12), bg="#C49B6D", fg="white",
                         relief=tk.RAISED, bd=2)
    back_btn.pack(side=tk.RIGHT, padx=20)
    
    form_frame = tk.Frame(parent, bg="#F5E6D3")
    form_frame.pack(fill=tk.BOTH, expand=True, padx=40, pady=40)
    
    name_label = tk.Label(form_frame, text="Item Name:", 
                          font=("Arial", 12), bg="#F5E6D3", fg="#4A3728")
    name_label.pack(anchor="w", pady=(10, 5))
    name_entry = tk.Entry(form_frame, font=("Arial", 14), 
                          relief=tk.SOLID, bd=2)
    name_entry.pack(fill=tk.X, pady=5)
    
    price_label = tk.Label(form_frame, text="Price ($):", 
                           font=("Arial", 12), bg="#F5E6D3", fg="#4A3728")
    price_label.pack(anchor="w", pady=(10, 5))
    price_entry = tk.Entry(form_frame, font=("Arial", 14), 
                           relief=tk.SOLID, bd=2)
    price_entry.pack(fill=tk.X, pady=5)
    
    # Production date field
    date_label = tk.Label(form_frame, text="Production Date (YYYY-MM-DD):", 
                          font=("Arial", 12), bg="#F5E6D3", fg="#4A3728")
    date_label.pack(anchor="w", pady=(10, 5))
    date_entry = tk.Entry(form_frame, font=("Arial", 14), 
                          relief=tk.SOLID, bd=2)
    date_entry.pack(fill=tk.X, pady=5)
    
    def on_create_clicked():
        """Called when user clicks Create button"""
        name = name_entry.get()
        try:
            price = float(price_entry.get())
            production_date = date_entry.get()
            
            success, msg = items_core.add_item(item_manager, name, price, production_date)
            
            if success:
                messagebox.showinfo("Success", msg)
                show_launch_screen(parent, item_manager)
            else:
                messagebox.showerror("Error", msg)
        except ValueError:
            messagebox.showerror("Error", "Invalid price or date format!")
    
    # Create button
    create_btn = tk.Button(form_frame, text="Create Item", 
                           command=on_create_clicked,
                           font=("Arial", 14, "bold"), 
                           bg="#28A745", fg="white",
                           width=20, height=2,
                           relief=tk.RAISED, bd=3)
    create_btn.pack(pady=30)
    
    parent.configure(bg="#F5E6D3")

